Imports System.Configuration
Imports System.Configuration.ConfigurationManager

<CLSCompliant(True)> _
Public Class CConfigBase





    'Utilities
    Protected Shared Function Config(ByVal key As String) As String
        Try
            Dim s As String = EncryptedSettings(key)
            If IsNothing(s) Then s = AppSettings(key)
            If IsNothing(s) Then Return String.Empty
            Return s
        Catch ex As Exception
            If IsNothing(ex.InnerException) Then Throw ex
            Throw ex.InnerException
        End Try
    End Function
    Protected Shared Function Config(ByVal key As String, ByVal defaultValue As String) As String
        Config = Config(key)
        If Len(Config) = 0 Then Config = defaultValue
    End Function
    Protected Shared Function ConfigOrEx(ByVal key As String) As String
        Dim s As String = Config(key)
        If Len(s) = 0 Then Throw New Exception("Missing Config setting: " & key)
        Return s
    End Function
    Protected Shared Function ConfigBool(ByVal key As String) As Boolean
        Return ConfigBool(key, False)
    End Function
    Protected Shared Function ConfigBool(ByVal key As String, ByVal defaultValue As Boolean) As Boolean
        Dim s As String = Config(key)
        If Len(s) = 0 Then Return defaultValue
        If 0 = String.Compare("true", s, True) Then Return True
        If 0 = String.Compare("yes", s, True) Then Return True
        Return False
    End Function
    Protected Shared Function ConfigInt(ByVal key As String) As Integer
        Return ConfigInt(key, Integer.MinValue)
    End Function
    Protected Shared Function ConfigInt(ByVal key As String, ByVal defaultValue As Integer) As Integer
        Dim s As String = Config(key)
        Dim i As Integer
        If Not Integer.TryParse(s, i) Then Return defaultValue
        Return i
    End Function
    Protected Shared Function ConfigLong(ByVal key As String) As Long
        Return ConfigLong(key, Long.MinValue)
    End Function
    Protected Shared Function ConfigLong(ByVal key As String, ByVal defaultValue As Long) As Long
        Dim s As String = Config(key)
        Dim i As Long
        If Not Long.TryParse(s, i) Then Return defaultValue
        Return i
    End Function
    Protected Shared Function ConfigGuid(ByVal key As String) As Guid
        Return ConfigGuid(key, Guid.Empty)
    End Function
    Protected Shared Function ConfigGuid(ByVal key As String, ByVal defaultValue As Guid) As Guid
        Dim s As String = Config(key)
        If s.Length = 36 OrElse s.Length = 38 Then
            Try
                Return New Guid(s)
            Catch
                Return defaultValue
            End Try
        End If
        Return defaultValue
    End Function


    '        <section name="encryptedSettings" type="System.Configuration.NameValueSectionHandler"/>
    '    </configSections>
    '   <encryptedSettings>
    '    <add key="EncryptionKey" value="asdfasdf" />
    '   </encryptedSettings>
    Private Shared m_checked As Boolean = False
    Private Shared m_hasEncr As Boolean = True
    Private Shared Function EncryptedSettings(ByVal key As String) As String
        If Not m_hasEncr Then Return Nothing
        Dim e As IEnumerable = CType(ConfigurationManager.GetSection("encryptedSettings"), IEnumerable)
        If IsNothing(e) Then
            m_hasEncr = False
            Return Nothing 'Section not found
        End If

        EncryptedSettings = CStr(e.GetType().GetProperties()(0).GetGetMethod().Invoke(e, New Object() {key}))
        If IsNothing(EncryptedSettings) Then Return Nothing 'Key not found

        If m_checked Then Exit Function

            CheckConfigIsEncrypted_NonWeb()


        m_checked = True
    End Function
    Private Shared Sub CheckConfigIsEncrypted_NonWeb()
        Dim c As Configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None)
        Dim cs As ConfigurationSection = c.GetSection("encryptedSettings")
        If IsNothing(cs) Then Exit Sub
        Dim si As SectionInformation = cs.SectionInformation
        If Not si.IsProtected Then
            si.ProtectSection("DataProtectionConfigurationProvider") 'RSA '"DPAPIProtectedConfigurationProvider")
            c.Save(ConfigurationSaveMode.Full)
        End If
    End Sub

End Class
